{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "808e2e8e-8ade-4795-98d9-37ff3e5981e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4de44005-0b12-487a-99c5-50d8853b7825",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_path = 'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a1d86b02-ac14-4a6c-95fb-410b805bf97e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取datasheet信息\n",
    "os.chdir(base_path)\n",
    "datasheet = pd.read_excel('DataSheet.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a2c7e2d2-a3a2-4bd7-8613-9295db317962",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取order1和order2的被试名单\n",
    "offline_order1_data = datasheet[datasheet['线下实验']==1]\n",
    "offline_order2_data = datasheet[datasheet['线下实验']==2]\n",
    "offline_order1_name = offline_order1_data['被试姓名'].tolist()\n",
    "offline_order2_name = offline_order2_data['被试姓名'].tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f172ac2d-9f5f-4ab3-ad3d-9407526880f1",
   "metadata": {},
   "source": [
    "## Order1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d49c2174-79e5-4ea9-9d4a-78ccc6118a78",
   "metadata": {},
   "outputs": [],
   "source": [
    "# offline_data_order1\n",
    "off_order1_path = f'{base_path}/NaoDao_DATA/OfflineDATA/CleanedDATA/Order1'\n",
    "os.chdir(off_order1_path)\n",
    "off_order1_task = os.listdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e8a87fb9-f6a6-4ded-9bf3-ec55fb5d14fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保留order1的被试数据\n",
    "for i in range(0,len(off_order1_task)):\n",
    "    off_order1_task_path = f'{off_order1_path}/{off_order1_task[i]}'\n",
    "    os.chdir(off_order1_task_path)\n",
    "    off_order1_filelists = os.listdir()\n",
    "    for m in range(0,len(off_order1_filelists)):\n",
    "        participant_name = off_order1_filelists[m].split('_')[0]\n",
    "        if participant_name not in offline_order1_name:\n",
    "            os.remove(off_order1_filelists[m])    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e6be8a76-4352-4e7c-947c-4d6c4b79aec2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将order1数据文件分到三个不同文件夹\n",
    "for i in range(0,len(off_order1_task)):\n",
    "    off_order1_task_path = f'{off_order1_path}/{off_order1_task[i]}'\n",
    "    os.chdir(off_order1_task_path)\n",
    "    # make three folders\n",
    "    folder1 = 'csv_file'\n",
    "    folder2 = 'log_file'\n",
    "    folder3 = 'psydat_file'\n",
    "    os.mkdir(folder1)\n",
    "    os.mkdir(folder2)\n",
    "    os.mkdir(folder3)\n",
    "    # get new filelists\n",
    "    off_order1_filelists_2 = os.listdir()\n",
    "    for n in range(0,len(off_order1_filelists_2)):\n",
    "        if '.csv' in off_order1_filelists_2[n]:\n",
    "            shutil.move(off_order1_filelists_2[n],'csv_file')\n",
    "        elif '.log' in off_order1_filelists_2[n]:\n",
    "            shutil.move(off_order1_filelists_2[n],'log_file')\n",
    "        elif '.psydat' in off_order1_filelists_2[n]:\n",
    "            shutil.move(off_order1_filelists_2[n],'psydat_file')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85092f0a-1afc-451c-8dbe-9deb1c5900a4",
   "metadata": {},
   "source": [
    "## Order2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1ca58e68-0f66-42ba-b5d1-d9aad1754706",
   "metadata": {},
   "outputs": [],
   "source": [
    "# offline_data_order2\n",
    "off_order2_path = f'{base_path}/NaoDao_DATA/OfflineDATA/CleanedDATA/Order2'\n",
    "os.chdir(off_order2_path)\n",
    "off_order2_task = os.listdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bd496348-6ab6-4b9f-81b1-17c3f7da4f59",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保留order2的被试数据\n",
    "for i in range(0,len(off_order2_task)):\n",
    "    off_order2_task_path = f'{off_order2_path}/{off_order2_task[i]}'\n",
    "    os.chdir(off_order2_task_path)\n",
    "    off_order2_filelists = os.listdir()\n",
    "    for m in range(0,len(off_order2_filelists)):\n",
    "        participant_name = off_order2_filelists[m].split('_')[0]\n",
    "        if participant_name not in offline_order2_name:\n",
    "            os.remove(off_order2_filelists[m])    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c67313e4-dc66-47ef-abd8-7615a02a4489",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将order2数据文件分到三个不同文件夹\n",
    "for i in range(0,len(off_order2_task)):\n",
    "    off_order2_task_path = f'{off_order2_path}/{off_order2_task[i]}'\n",
    "    os.chdir(off_order2_task_path)\n",
    "    # make three folders\n",
    "    folder1 = 'csv_file'\n",
    "    folder2 = 'log_file'\n",
    "    folder3 = 'psydat_file'\n",
    "    os.mkdir(folder1)\n",
    "    os.mkdir(folder2)\n",
    "    os.mkdir(folder3)\n",
    "    # get new filelists\n",
    "    off_order2_filelists_2 = os.listdir()\n",
    "    for n in range(0,len(off_order2_filelists_2)):\n",
    "        if '.csv' in off_order2_filelists_2[n]:\n",
    "            shutil.move(off_order2_filelists_2[n],'csv_file')\n",
    "        elif '.log' in off_order2_filelists_2[n]:\n",
    "            shutil.move(off_order2_filelists_2[n],'log_file')\n",
    "        elif '.psydat' in off_order2_filelists_2[n]:\n",
    "            shutil.move(off_order2_filelists_2[n],'psydat_file')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
